<?php
/**
 * User: liubao
 * Date: 19-4-30
 * Time: 下午4:43
 */

namespace App\Service;



use App\Model\User;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;

class UserService extends BaseService
{
    /**
     * @param $params
     * @return array|bool|mixed|string
     */
    public static function doLogin($params)
    {
        $username = $params['username'];
        $password = $params['password'];
        $userModel = self::userModel();
        $res = $userModel->login($username,$password);
        //unset($res->password);
        if (!empty($res)){
            $res->token = "admin-token";
            $session = self::session();
            $session->start();
            $session->set('userInfo', $res);
        }
        return $res;
    }

    /**
     * @param $name //清除session属性
     * @return bool
     */
    public static function clearSession($name)
    {
        $session = self::session();
        if ($name){
            $session->remove($name);
        }else{
            $session->clear();
        }
        return true;
    }

    protected static function userModel()
    {
        return new User();
    }

    protected static function session()
    {
        return new Session(new PhpBridgeSessionStorage());
    }

}
